import {ApiClient} from './api'
import {createMessageConfig} from '@/utils/responseHandler'

// 权限相关类型定义
export interface PermissionLevel {
  level: number
  code: string
  displayName: string
  description: string
}

export interface Permission {
  id: number
  userId: number
  username: string
  userDisplayName: string
  productId: number
  productCode: string
  productName: string
  permissionLevel: number
  permissionLevelDisplayName: string
  permissionLevelDescription: string
  assignedById?: number
  assignedByUsername?: string
  assignedByDisplayName?: string
  remark?: string
  createTime: string
  updateTime: string
}

export interface PermissionStats {
  ownerCount: number
  collaboratorCount: number
  reporterCount: number
  totalCount: number
}

export interface AssignPermissionRequest {
  userId: number
  productId: number
  permissionLevel: number
  remark?: string
}

export interface UpdatePermissionRequest {
  permissionLevel: number
  remark?: string
}

export interface SearchPermissionsRequest {
  userId?: number
  productId?: number
  permissionLevel?: number
  page?: number
  size?: number
  sort?: string
  direction?: string
}

export interface PageResponse<T> {
  content: T[]
  totalElements: number
  totalPages: number
  size: number
  number: number
  first: boolean
  last: boolean
}


// 权限管理API
export const permissionService = {
  /**
   * 分配产品权限
   */
  assignPermission: (data: AssignPermissionRequest) => {
    return ApiClient.post<Permission>('/auth/permissions', data, createMessageConfig('权限分配成功'))
  },

  /**
   * 修改用户产品权限
   */
  updatePermission: (
    userId: number,
    productId: number,
    data: UpdatePermissionRequest
  ) => {
    return ApiClient.put<Permission>(`/auth/permissions/${userId}/${productId}`, data, createMessageConfig('权限修改成功'))
  },

  /**
   * 回收用户产品权限
   */
  revokePermission: (userId: number, productId: number) => {
    return ApiClient.delete<void>(`/auth/permissions/${userId}/${productId}`, createMessageConfig('权限回收成功'))
  },

  /**
   * 获取用户在指定产品的权限
   */
  getUserProductPermission: (userId: number, productId: number) => {
    return ApiClient.get<Permission>(`/auth/permissions/${userId}/${productId}`, { showSuccess: false })
  },

  /**
   * 获取用户所有产品权限
   */
  getUserPermissions: (userId: number) => {
    return ApiClient.get<Permission[]>(`/auth/permissions/user/${userId}`, { showSuccess: false })
  },

  /**
   * 获取产品的所有用户权限
   */
  getProductPermissions: (productId: number) => {
    return ApiClient.get<Permission[]>(`/auth/permissions/product/${productId}`, { showSuccess: false })
  },

  /**
   * 分页查询用户产品权限
   */
  searchPermissions: (params: SearchPermissionsRequest) => {
    return ApiClient.get<PageResponse<Permission>>('/auth/permissions', { params, showSuccess: false })
  },

  /**
   * 获取权限统计信息
   */
  getPermissionStats: (productId: number) => {
    return ApiClient.get<PermissionStats>(`/auth/permissions/product/${productId}/stats`, { showSuccess: false })
  },

  /**
   * 获取权限级别列表
   */
  getPermissionLevels: () => {
    return ApiClient.get<PermissionLevel[]>('/auth/permissions/levels', { showSuccess: false })
  }
}

// 导出具体方法以便按需导入
export const {
  assignPermission,
  updatePermission,
  revokePermission,
  getUserProductPermission,
  getUserPermissions,
  getProductPermissions,
  searchPermissions,
  getPermissionStats,
  getPermissionLevels
} = permissionService